<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="登录平台" prop="platform">
        <el-input
          v-model="queryParams.platform"
          placeholder="请输入登录平台"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status"  clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评论状态 " prop="commentStatus">
        <el-select v-model="queryParams.commentStatus" clearable size="small">
          <el-option
            v-for="dict in dict.type.user_comment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="邮件通知" prop="emailNoticeStatus">
        <el-select v-model="queryParams.emailNoticeStatus" clearable size="small">
          <el-option
            v-for="dict in dict.type.email_notice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" clearable size="small">
          <el-option
            v-for="dict in dict.type.web_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="登录时间" prop="loginDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.loginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择最后登录时间">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['web:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['web:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['web:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="webUserList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" align="center" fixed>
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId" v-if="false"/>
      <el-table-column label="用户昵称" align="center" prop="nickName" width="120"/>
      <el-table-column label="用户账号" align="center" prop="userName" width="120"/>
      <el-table-column label="头像" align="center" prop="avatar" width="80">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img :src="scope.row.avatar" height="100" />
            <el-avatar slot="reference" fit="scale-down" :src="scope.row.avatar"></el-avatar>
            <!-- <img slot="reference" :src="scope.row.avatar" style="width:30px; height:30px; border-radius:50%"/> -->
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" align="center" prop="userType" width="120">
          <template slot-scope="scope">
              <dict-tag :options="dict.type.web_user_type" :value="scope.row.userType"/>
          </template>
      </el-table-column>
      <el-table-column label="登录平台" align="center" prop="platform" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="评论状态" align="center" prop="commentStatus">
          <template slot-scope="scope">
              <dict-tag :options="dict.type.user_comment_status" :value="scope.row.commentStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="邮件通知" align="center" prop="emailNoticeStatus" width="120">
          <template slot-scope="scope">
              <dict-tag :options="dict.type.email_notice_status" :value="scope.row.emailNoticeStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      

      <el-table-column label="QQ号" align="center" prop="qqNumber" width="120"/>
      <el-table-column label="微信号" align="center" prop="weChat" width="120"/>
      
      <el-table-column label="邮箱" align="center" prop="email" width="120"/>
      <el-table-column label="电话号码" align="center" prop="phone" width="120"/>
      <el-table-column label="生日" align="center" prop="birthday" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="登录次数" align="center" prop="loginCount" />
      <el-table-column label="登录IP" align="center" prop="loginIp" width="150"/>
      <el-table-column label="登录时间" align="center" prop="loginDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览器" align="center" prop="browser" />
      <el-table-column label="操作系统" align="center" prop="os" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户端用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
            <el-col :span="15">
                <el-form-item label="用户昵称" prop="nickName">
                    <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
                </el-form-item>
                <el-form-item label="用户账号" prop="userName">
                    <el-input v-model="form.userName" placeholder="请输入用户名" />
                </el-form-item>
                
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in dict.type.sys_normal_disable"
                            :key="dict.value"
                            :label="parseInt(dict.value)"
                            border
                            >{{dict.label}}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="用户类型" prop="userType">
                    <el-select v-model="form.userType">
                        <el-option
                            v-for="dict in dict.type.web_user_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="parseInt(dict.value)"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="9">
                <el-form-item label="头像">
                    <imageUpload v-model="form.avatar" :limit="imageLimit" :isShowTip="false"/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="登录平台" prop="platform">
          <el-input v-model="form.platform" placeholder="请输入登录平台" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="评论状态 ">
              <el-radio-group v-model="form.commentStatus">
                <el-radio
                    v-for="dict in dict.type.user_comment_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                    border
                    >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮件通知">
              <el-radio-group v-model="form.emailNoticeStatus">
                <el-radio
                    v-for="dict in dict.type.email_notice_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                    border
                    >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
        </el-row>

        <el-row>
            <el-col :span="12">
              <el-form-item label="用户性别" prop="sex">
                <el-select v-model="form.sex" placeholder="请选择用户性别">
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生日" prop="birthday">
                <el-date-picker clearable size="small"
                  v-model="form.birthday"
                  type="date"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>
        
        <el-form-item label="QQ号" prop="qqNumber">
          <el-input v-model="form.qqNumber" placeholder="请输入QQ号" />
        </el-form-item>
        <el-form-item label="微信号" prop="weChat">
          <el-input v-model="form.weChat" placeholder="请输入微信号" />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话号码" />
        </el-form-item>
        
        <el-form-item label="自我简介" prop="summary">
          <el-input type="textarea" v-model="form.summary" placeholder="最多150字" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWebUser, getWebUser, delWebUser, addWebUser, updateWebUser } from "@/api/blog/webUser";

export default {
  name: "WebUser",
  dicts: ['sys_normal_disable', 'sys_user_sex','user_comment_status', 'email_notice_status','web_user_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户端用户表格数据
      webUserList: [],
      // 弹出层标题
      title: "",
      // 封面数量限制
      imageLimit: 1,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        nickName: undefined,
        platform: undefined,
        status: undefined,
        commentStatus: undefined,
        emailNoticeStatus: undefined,
        sex: undefined,
        userType: undefined,
        loginDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        platform: [
          { required: true, message: "登录平台不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        commentStatus: [
          { required: true, message: "评论状态不能为空", trigger: "blur" }
        ],
        emailNoticeStatus: [
          { required: true, message: "邮件通知状态不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "用户性别不能为空", trigger: "change" }
        ],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户端用户列表 */
    getList() {
      this.loading = true;
      listWebUser(this.queryParams).then(response => {
        this.webUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        userName: undefined,
        nickName: undefined,
        platform: undefined,
        status: 1,
        commentStatus: 1,
        emailNoticeStatus: 0,
        avatar: undefined,
        qqNumber: undefined,
        weChat: undefined,
        summary: undefined,
        sex: 2,
        email: undefined,
        phone: undefined,
        birthday: undefined,
        userType: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const userId = row.userId || this.ids
      getWebUser(userId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.userId != null) {
            updateWebUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addWebUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除客户端用户编号为"' + userIds + '"的数据项？').then(() => {
        this.loading = true;
        return delWebUser(userIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
        this.$download.excel('/blog/webUser/export', this.queryParams);
    }
  }
};
</script>